<?php

namespace pmvc\mvc\view;

use pmvc\mvc\ViewHandler;
use pmvc\mvc\Dispatcher;
use pmvc\mvc\Model;

class SmartyViewHandler 
    implements ViewHandler {

    public $compileDir;
    public $configDir;
    public $cacheDir;
    public $useCache;
    public $templateDir;
    public $hooks;
    public $hookFile;
    
    private $smarty;
    private $file;
    
    /**
     * Creates this shit
     */
	public  function renderView(Model $model, $viewName, Dispatcher $dispatcher) {
		
		// bring in smarty
		pmvc_requireFile("/smarty/Smarty.class.php");
    	
		// create needed directories
		if (!file_exists($this->compileDir) 
			&& !mkdir($this->compileDir, 755, TRUE)) {
			\pmvc\log\Log::error("Unable to create compile dir: {0}", $this->compileDir);
		}
		if (!file_exists($this->configDir) 
			&& !mkdir($this->configDir, 755, TRUE)) {
			\pmvc\log\Log::error("Unable to create config dir: {0}", $this->configDir);
		}
		if (!file_exists($this->cacheDir) 
			&& !mkdir($this->cacheDir, 755, TRUE)) {
			\pmvc\log\Log::error("Unable to create cache dir: {0}", $this->cacheDir);
		}

        // create
        $this->smarty = new \Smarty();
        
        // configure
        $this->smarty->template_dir = $this->templateDir;
        $this->smarty->compile_dir  = $this->compileDir;
        $this->smarty->config_dir   = $this->configDir;
        $this->smarty->cache_dir    = $this->cacheDir;
        $this->smarty->caching	    = strtolower($this->useCache)=="true";
        $this->smarty->use_sub_dirs = TRUE;
        
        // set variables
        $this->smarty->assign(Array(
            "request"	    => $_REQUEST,
            "post"	        => $_POST,
            "get"	        => $_GET,
            "session"       => $_SESSION,
            "server"	    => $_SERVER,
            "dispatcher"	=> $dispatcher
        ));
        
        // bring in the functions 
        pmvc_requireFile("/pmvc/mvc/view/smarty_functions.php");
        
        // bring in the hook file
        if (pmvc_hasVal($this->hookFile)) {
			pmvc_requireFile($this->hookFile);
        }
        
        // retgister functions
        $this->smarty->register_function(
        	"pmvc_getContextUrl", "smarty_pmvc_getContextUrl", false);
        $this->smarty->register_function(
        	"pmvc_getRequestedUrl", "smarty_pmvc_getRequestedUrl", false);
        $this->smarty->register_function(
        	"pmvc_getAttribute", "smarty_pmvc_getAttribute", false);
        $this->smarty->register_function(
        	"pmvc_formInput", "smarty_pmvc_formInput", false);
        $this->smarty->register_function(
        	"pmvc_formSelect", "smarty_pmvc_formSelect", false);
        $this->smarty->register_function(
        	"pmvc_formTextArea", "smarty_pmvc_formTextArea", false);
        $this->smarty->register_function(
        	"pmvc_formInputCheckbox", "smarty_pmvc_formInputCheckbox", false);
        $this->smarty->register_function(
        	"pmvc_formInputPassword", "smarty_pmvc_formInputPassowrd", false);
        $this->smarty->register_function(
        	"pmvc_displayFormGlobalErrors", "smarty_pmvc_displayFormGlobalErrors", false);
        $this->smarty->register_function(
        	"pmvc_displayFormFieldErrors", "smarty_pmvc_displayFormFieldErrors", false);
        $this->smarty->register_function(
        	"pmvc_niceDate", "smarty_pmvc_niceDate", false);
			
		// register blocks
		$this->smarty->register_block('pmvc_dynamic', 'smarty_pmvc_block_dynamic', false);
		
		// let something hook
		if (pmvc_hasVal($this->hooks)) {
			foreach ($this->hooks as $hook) {
				$hook->hook($this->smarty, $dispatcher);
			}
		}

		// legacy hooks
		if (function_exists("pmvc_smarty_hook")) {
			pmvc_smarty_hook($this->smarty, $dispatcher);
		}
		
        // put the model into the template
        $this->smarty->assign($model->getArrayCopy());
        
        // render the view
		fwrite($dispatcher->getOutputStream(), $this->smarty->fetch($viewName));
    }
}

?>
